package com.trip_memories;

import java.util.UUID;

import signedUser.ChosedAlbum;
import signedUser.Signed;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import blogic.AccesBussineLogic;
import blogic.BusinessLogic;
import blogic.Operations;
import dbmodels.Photo;
import dbtypes.PermisionPhoto;

public class SavePictureInDb extends Activity {

	private byte[] imagedata;
	BusinessLogic bl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_save_picture_in_db);

		bl = AccesBussineLogic.getBussinesLogic(this);
		getDatas();

		ImageView img = (ImageView) findViewById(R.id.photo);
		img.setImageBitmap(Operations.convertFromBytesInBitmap(this, imagedata, 0));
		
		String autor = bl.UserName(Signed.getId());
		String album = bl.AlbumName(ChosedAlbum.getId());
		
		((TextView)findViewById(R.id.author_name)).setText(autor);
		((TextView)findViewById(R.id.album_name)).setText(album);

	}

	private void getDatas() {
		try {
			imagedata = getIntent().getExtras().getByteArray("bytes");

		} catch (Exception e) {

		}
	}

	public boolean onSave(View v) {
		Photo p = new Photo();

		p.setBytes(imagedata);
		p.setId(UUID.randomUUID());
		p.setAlbumid(ChosedAlbum.getId());

		String name = ((EditText) findViewById(R.id.namePhotoSave)).getText()
				.toString();

		p.setName(name);

		int radioid = ((RadioGroup) findViewById(R.id.permisionPhoto))
				.getCheckedRadioButtonId();
		p.setPermision(PermisionPhoto.Nothing);
		switch (radioid) {
		case R.id.nothing_photo: {
			p.setPermision(PermisionPhoto.Nothing);
			break;
		}
		case R.id.all_photo: {
			p.setPermision(PermisionPhoto.All);
			break;
		}
		}

		if (p.isValid()) {
			bl.AddPhoto(p);
			finish();
		} else {
			dialog();
		}

		return true;
	}

	public void dialog(){
		Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.photo_wrong);
		builder.setPositiveButton(android.R.string.cancel, null);
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public boolean onCancel(View v) {
		finish();
		return true;
	}

}
